/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.free.ts.web;

import be.free.ts.activity.web.ActivityPlace;
import be.free.ts.contact.web.ContactPlace;
import be.free.ts.customer.web.CustomerPlace;
import be.free.ts.employee.web.EmployeePlace;
import be.free.ts.project.web.ProjectPlace;
import be.free.ts.support.web.AsyncBasicPresenter;
import be.free.ts.timesheet.web.TimesheetPlace;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.inject.client.AsyncProvider;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;

/**
 * @author Christophe De Blende
 * @since 06/01/13
 */
public class MainPresenter extends AsyncBasicPresenter<MainPresenter.Display> implements ClickHandler {

	public interface Display extends WidgetDisplay {
		Panel getContent();

		HasClickHandlers activityLink();

		HasClickHandlers contactLink();

		HasClickHandlers customerLink();

		HasClickHandlers employeeLink();

		HasClickHandlers projectLink();

		HasClickHandlers timesheetLink();
	}

	@Inject
	public MainPresenter(AsyncProvider<Display> display, EventBus eventBus) {
		super(display, eventBus);
	}

	@Override
	protected void onBind() {
		display.activityLink().addClickHandler(this);
		display.contactLink().addClickHandler(this);
		display.customerLink().addClickHandler(this);
		display.employeeLink().addClickHandler(this);
		display.projectLink().addClickHandler(this);
		display.timesheetLink().addClickHandler(this);
		revealDisplay();
	}

	@Override
	protected void onUnbind() {
	}

	@Override
	protected void onRevealDisplay() {
		RootPanel rootPanel = RootPanel.get();
		rootPanel.clear();
		rootPanel.add(display.asWidget());
	}

	public void onClick(ClickEvent event) {
		Widget source = (Widget) event.getSource();

		if (source.getParent() == display.activityLink()) {
			eventBus.fireEvent(new PlaceRequestEvent(new PlaceRequest(ActivityPlace.PLACE)));

		} else if (source.getParent() == display.contactLink()) {
			eventBus.fireEvent(new PlaceRequestEvent(new PlaceRequest(ContactPlace.PLACE)));

		} else if (source.getParent() == display.customerLink()) {
			eventBus.fireEvent(new PlaceRequestEvent(new PlaceRequest(CustomerPlace.PLACE)));

		} else if (source.getParent() == display.employeeLink()) {
			eventBus.fireEvent(new PlaceRequestEvent(new PlaceRequest(EmployeePlace.PLACE)));

		} else if (source.getParent() == display.projectLink()) {
			eventBus.fireEvent(new PlaceRequestEvent(new PlaceRequest(ProjectPlace.PLACE)));

		} else if (source.getParent() == display.timesheetLink()) {
			eventBus.fireEvent(new PlaceRequestEvent(new PlaceRequest(TimesheetPlace.PLACE)));
		}

	}

}
